package com.bac.rentmap.web.controller.ad.manage;

import com.bac.rentmap.ad.flat.FlatAd;
import com.bac.rentmap.ad.flat.FlatAdDao;
import com.bac.rentmap.ad.flat.crawler.FlatCrawledAd;
import com.bac.rentmap.ad.flat.crawler.FlatCrawledAdDao;
import com.bac.rentmap.ad.flat.rating.BetterFlatAdProvider;
import com.bac.rentmap.ad.flat.rating.FlatAdRatingCalculator;
import com.bac.rentmap.ad.flat.rating.update.NormalizedRatingIncrementEstimatingService;
import com.bac.rentmap.ad.flat.rating.update.NormalizedRatingIncrementEstimation;
import com.bac.rentmap.ad.flat.rating.update.RatingAwareFlatAdUpdater;
import com.bac.rentmap.web.controller.ad.add.AddFlatController;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.logging.Logger;

public class FlatAdStatisticsController extends MultiActionController {
    private static final Logger log = Logger
            .getLogger(FlatAdStatisticsController.class.getName());
    private FlatAdDao flatAdDao;
    private NormalizedRatingIncrementEstimatingService normalizedRatingIncrementEstimatingService;
    private RatingAwareFlatAdUpdater lastPublishingDateUpdater;
    private FlatAdRatingCalculator flatAdRatingCalculator;
    private BetterFlatAdProvider betterFlatAdProvider;

    public ModelAndView view(HttpServletRequest request,
                             HttpServletResponse response) throws Exception {
        String secureId = request.getParameter("secureId");
        log.fine("About to view flatAd, secureId: " + secureId);
        FlatAd flatAd = flatAdDao.getBySecureId(secureId);
        ModelAndView res = new ModelAndView("ad/manage/statistics/statistics");

        if(flatAd != null) {
            res.addObject("flatAd", flatAd);
            if(flatAd.getRatingCalculated()) {
                List<NormalizedRatingIncrementEstimation> ratingIncrementEstimations = normalizedRatingIncrementEstimatingService
                        .estimate(secureId);
                res.addObject("ratingIncrementEstimations", ratingIncrementEstimations);
                long rating = flatAd.getRating();
                res.addObject("betterFlatAdCount", betterFlatAdProvider
                        .countFlatAdsWithBetterRating(flatAd.getCity(), rating));
                res.addObject("normalizedRating", flatAdRatingCalculator
                        .normalizeRating(rating));
            }
        }
        return res;
    }

    public ModelAndView updateLastPublishingDate(HttpServletRequest request,
                                                 HttpServletResponse response) throws Exception {
        String secureId = request.getParameter("secureId");
        log.fine("About to update LastPublishingDate, secureId: " + secureId);
        lastPublishingDateUpdater.update(secureId);
        return view(request, response);
    }

    @Required
    public void setFlatAdDao(FlatAdDao flatAdDao) {
        this.flatAdDao = flatAdDao;
    }

    @Required
    public void setNormalizedRatingIncrementEstimatingService(
            NormalizedRatingIncrementEstimatingService normalizedRatingIncrementEstimatingService) {
        this.normalizedRatingIncrementEstimatingService = normalizedRatingIncrementEstimatingService;
    }

    @Required
    public void setLastPublishingDateUpdater(
            RatingAwareFlatAdUpdater lastPublishingDateUpdater) {
        this.lastPublishingDateUpdater = lastPublishingDateUpdater;
    }

    @Required
    public void setFlatAdRatingCalculator(
            FlatAdRatingCalculator flatAdRatingCalculator) {
        this.flatAdRatingCalculator = flatAdRatingCalculator;
    }

    @Required
    public void setBetterFlatAdProvider(
            BetterFlatAdProvider betterFlatAdProvider) {
        this.betterFlatAdProvider = betterFlatAdProvider;
    }}
